package com.fuang.rabbitmq.configure;

import com.google.common.collect.ImmutableMap;
import com.rabbitmq.client.AMQP;
import groovy.transform.Immutable;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ测试配置类
 * Created by Fuang on 2018-1-6.
 */
@Configuration
public class RabbitConfig {

    @Bean
    public Queue helloQueue(){
        return new Queue("hello");
    }

    // 测试topic
    @Bean
    public Queue topicAQueue(){
        return  new Queue("topicAQueue");
    }

    @Bean
    public Queue topicBQueue(){
        return  new Queue("topicBQueue");
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topicExchange", true, false);
    }

    /**
     * 符号“#”匹配一个或多个词，符号“*”匹配一个词
     * @return
     */
    @Bean
    public Binding topicABinding(Queue topicAQueue, TopicExchange topicExchange){
        return BindingBuilder.bind(topicAQueue).to(topicExchange).with("topic.#");
    }

    @Bean
    public Binding topicBBinding(Queue topicBQueue, TopicExchange topicExchange){
        return BindingBuilder.bind(topicBQueue).to(topicExchange).with("#.queue");
    }

    @Bean
    public Queue fanoutAQueue(){
        return new Queue("fanoutAQueue");
    }


    // 测试fanout
    @Bean
    public Queue fanoutBQueue(){
        return new Queue("fanoutBQueue");
    }

    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutExchange", true, false);
    }

    @Bean
    public Binding fanoutABinding(Queue fanoutAQueue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutAQueue).to(fanoutExchange);
    }

    @Bean
    public Binding fanoutBBinding(Queue fanoutBQueue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutBQueue).to(fanoutExchange);
    }

    @Bean
    public Queue batchQueue(){
        return new Queue("batchQueue", true, false, false, null);
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("directExchange", true, false);
    }

    @Bean
    public Binding batchBinding() {
        return BindingBuilder.bind(batchQueue()).to(directExchange()).with("batch-queue");
    }

    /**
     * 延迟队列实现思路：
     * 1，设置队列消息的过期时间
     * 2，消息过期后失效的消息转入死信队列
     * 3，消费私信队列
     * @return
     */
    @Bean
    public Queue delayQueue() {
        return new Queue("delayQueue", true, false, false, ImmutableMap.of(
                "x-message-ttl", 10000,  // 设置过期时间
                "x-dead-letter-exchange", "deadLetterExchange", // 设置死信队列绑定的Exchange
                "x-dead-letter-routing-key", "dead-letter-key")); // 设置死信队列的route-key
    }

    @Bean
    public DirectExchange delayExchange() {
        return new DirectExchange("delayExchange", true, false);
    }

    @Bean
    public Binding delayBinding() {
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with("delay-key");
    }

    @Bean
    public Queue deadLetterQueue() {
        return new Queue("deadLetterQueue", true, false, false);
    }

    @Bean
    public DirectExchange deadLetterExchange(){
        return new DirectExchange("deadLetterExchange", true, false);
    }

    @Bean
    public Binding deadLetterBinding(){
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with("dead-letter-key");
    }

}
